<template>
  <div class="component-upload-image">
    <el-upload
      :action="uploadImgVideoUrl"
      list-type="picture-card"
      :on-success="handleUploadSuccess"
      :before-upload="handleBeforeUpload"
      :on-error="handleUploadError"
      name="file"
      :show-file-list="false"
      :headers="headers"
      style="display: inline-block; vertical-align: top"
      :disabled="currentHandle == '详情'"
    >
      <div slot="tip" class="el-upload__tip" v-if="adType == '1'">
        图片尺寸控制 1080*1920
      </div>
      <el-image v-if="!valueUrl" :src="valueUrl">
        <div slot="error" class="image-slot">
          <i class="el-icon-plus" />
        </div>
      </el-image>
      <div v-else class="image">
        <el-image
          :src="valueUrl"
          :style="`width:150px;height:150px;`"
          fit="fill"
          v-if="adType == '1'"
        />
        <video
          :src="valueUrl"
          :style="`width:150px;height:150px;`"
          fit="fill"
          v-if="adType == '2'"
          controls="controls"
        />
        <div class="mask" v-if="adType == '1'">
          <div class="actions">
            <span title="预览" @click.stop="dialogVisible = true">
              <i class="el-icon-zoom-in" />
            </span>
            <span
              title="移除"
              @click.stop="removeImageVideo"
              v-if="currentHandle != '详情'"
            >
              <i class="el-icon-delete" />
            </span>
          </div>
        </div>
      </div>
    </el-upload>
    <el-dialog
      :visible.sync="dialogVisible"
      title="预览"
      width="800"
      append-to-body
    >
      <img
        :src="valueUrl"
        style="display: block; max-width: 100%; margin: 0 auto"
      />
    </el-dialog>
  </div>
</template>

<script>
import { getToken } from "@/utils/auth";

export default {
  data () {
    return {
      dialogVisible: false,
      uploadImgVideoUrl: process.env.VUE_APP_BASE_API + "/uploadImage", // 上传的图片服务器地址
      headers: {
        Authorization: "Bearer " + getToken(),
      },
      valueUrl: ''
    };
  },
  props: {
    value: {
      type: String
    },
    adType: {
      type: String,
      default: "",
    },
    currentHandle: {
      type: String,
      default: "",
    }
  },
  created () {
    if (this.value) {
      this.valueUrl = process.env.VUE_APP_BASE_API + this.value;
    }
  },
  methods: {
    removeImageVideo () {
      this.valueUrl = ''
      this.$emit("input", "");
    },
    handleUploadSuccess (res) {
      // console.log(res, 'res')
      this.$emit("input", res.data);
      this.valueUrl = process.env.VUE_APP_BASE_API + res.data;
    },
    handleBeforeUpload (file) {
      //上传图片
      if (this.adType == '1') {
        const isJpg = /image\/(png|jpe?g|gif)$/.test(file.type);
        // const isLt2M = file.size / 1024 / 1024 < 2;
        if (!isJpg) {
          this.$message.error('上传图片只能是 JPG/PNG 格式!');
        }
        // if (!isLt2M) {
        //   this.$message.error('上传图片大小不能超过 2MB!');
        // }
        const isSize = new Promise(function (resolve, reject) {
          let width = 1080;
          let height = 1920;
          let _URL = window.URL || window.webkitURL;
          let img = new Image();
          img.onload = function () {
            let valid = img.width == width && img.height == height;
            valid ? resolve() : reject();
          }
          img.src = _URL.createObjectURL(file);
        }).then(() => {
          return file;
        }, () => {
          this.$message.error('上传图片的尺寸不对');
          return Promise.reject()
        })
        return isJpg && isSize;
      } else if (this.adType == '2') {
        const isVideo = file.type == "video/mp4" || file.type == "video/webm" || file.type == "video/swf" ||
          file.type == "video/flv" || file.type == "video/mp3" || file.type == "video/wav" ||
          file.type == "video/wma" || file.type == "video/wmv" || file.type == "video/mid" ||
          file.type == "video/avi" || file.type == "video/mpg" || file.type == "video/asf" ||
          file.type == "video/rm" || file.type == "video/rmvb";
        const isLt10M = file.size / 1024 / 1024 < 10;
        if (!isVideo) {
          this.$message.error("请上传视频文件");
          return false;
        }
        if (!isLt10M) {
          this.$message.error("上传视频文件不能大于10M");
          return false;
        }
      } else {
        this.$message.error('请先选择上传文件类型');
        return false;
      }
    },
    handleUploadError () {
      this.$message({
        type: "error",
        message: "上传失败",
      });
    },
  },
  watch: {
    value () {
      if (this.value) {
        this.valueUrl = process.env.VUE_APP_BASE_API + this.value;
      }
    }
  },
};
</script>

<style scoped lang="scss">
.image {
  position: relative;
  .mask {
    opacity: 0;
    position: absolute;
    top: 0;
    width: 150px;
    height: 150px;
    background-color: rgba(0, 0, 0, 0.5);
    transition: all 0.3s;
  }
  &:hover .mask {
    opacity: 1;
  }
}
</style>
